
package com.wyzz.hr.bo.impl;

import java.util.List;
import org.hibernate.criterion.*;

import com.wyzz.hr.base.Constants;
import com.wyzz.hr.bo.IRecruitchannelBo;
import com.wyzz.hr.dao.IRecruitchannelDAO;
import com.wyzz.hr.dao.IStatusDAO;
import com.wyzz.hr.pojo.Recruitchannel;
import com.wyzz.hr.util.Pager;

public class RecruitchannelBoImpl implements IRecruitchannelBo, Constants {

	public RecruitchannelBoImpl() {
	}

	public Boolean deleteChannel(String id) {
		try {
			Recruitchannel recruitchannel = (Recruitchannel) channelDAO
					.loadObject(com.wyzz.hr.pojo.Recruitchannel.class, id,
							null, new boolean[0]);
			channelDAO.deleteObject(recruitchannel);
			return Boolean.valueOf(true);
		} catch (Exception e) {
			return Boolean.valueOf(false);
		}
	}

	public List getObjects(Class clas, String fetch[]) {
		return channelDAO.getObjects(clas, fetch);
	}

	public Boolean insertChannel(Recruitchannel channel) {
		try {
			channel.setRecchSortId(getMaxSortId());
			channelDAO.saveObject(channel);
			return Boolean.valueOf(true);
		} catch (Exception e) {
			return Boolean.valueOf(false);
		}
	}

	public List searchChannel(Recruitchannel channel, Pager page) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Recruitchannel.class);
		detachedCriteria.addOrder(Order.asc(Recruitchannel.PROP_RECCH_SORT_ID));
		List result = channelDAO.findByCriteria(detachedCriteria);
		return result;
	}

	public Integer searchChannelCountByName(String channelName) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Recruitchannel.class);
		detachedCriteria.add(Restrictions.eq(Recruitchannel.PROP_RECCH_NAME,
				channelName));
		Integer count = Integer.valueOf(channelDAO
				.findRowCountByCriteria(detachedCriteria));
		return count;
	}

	public Recruitchannel searchChannelByName(String channelName) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Recruitchannel.class);
		detachedCriteria.add(Restrictions.eq(Recruitchannel.PROP_RECCH_NAME,
				channelName));
		List list = channelDAO.findByCriteria(detachedCriteria);
		if (list.isEmpty())
			return null;
		else
			return (Recruitchannel) list.get(0);
	}

	public Boolean updateChannel(Recruitchannel channel) {
		Recruitchannel oldRecruitchannel = (Recruitchannel) channelDAO
				.loadObject(com.wyzz.hr.pojo.Recruitchannel.class, channel
						.getId(), null, new boolean[0]);
		oldRecruitchannel.setRecchAddr(channel.getRecchAddr());
		oldRecruitchannel.setRecchAddrZip(channel.getRecchAddrZip());
		oldRecruitchannel.setRecchCityNo(channel.getRecchCityNo());
		oldRecruitchannel.setRecchComments(channel.getRecchComments());
		oldRecruitchannel.setRecchEmail(channel.getRecchEmail());
		oldRecruitchannel.setRecchName(channel.getRecchName());
		oldRecruitchannel.setRecchPhone(channel.getRecchPhone());
		oldRecruitchannel.setRecchRespPerson(channel.getRecchRespPerson());
		channelDAO.updateObject(oldRecruitchannel);
		return Boolean.valueOf(true);
	}

	public void saveRecchOrder(String ids[]) {
		if (ids == null || ids.length == 0)
			return;
		int sort = 1;
		int len = ids.length;
		for (int i = 0; i < len; i++) {
			channelDAO.exeHql((new StringBuilder()).append(
					"update Recruitchannel set recchSortId=").append(sort)
					.append(" where id ='").append(ids[i]).append("'")
					.toString());
			sort++;
		}

	}

	public synchronized Integer getMaxSortId() {
		String hql = "select max(recchSortId) as maxSortId from Recruitchannel recch";
		List maxNoList = channelDAO.exeHqlList(hql);
		Integer maxID;
		if (maxNoList == null || maxNoList.size() < 1
				|| maxNoList.get(0) == null)
			maxID = Integer.valueOf(1);
		else
			maxID = Integer.valueOf(1 + ((Integer) (Integer) maxNoList.get(0))
					.intValue());
		return maxID;
	}

	public List loadAllChannel() {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Recruitchannel.class);
		return channelDAO.findByCriteria(detachedCriteria);
	}

	public IRecruitchannelDAO getChannelDAO() {
		return channelDAO;
	}

	public void setChannelDAO(IRecruitchannelDAO channelDAO) {
		this.channelDAO = channelDAO;
	}

	public IStatusDAO getStatusDAO() {
		return statusDAO;
	}

	public void setStatusDAO(IStatusDAO statusDAO) {
		this.statusDAO = statusDAO;
	}

	private IRecruitchannelDAO channelDAO;
	private IStatusDAO statusDAO;
}
